/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.menu.web.MenuAction
   Module Description   :

   Date Created      : 2010/5/24
   Original Author   : ellen.li
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.menu.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;

/**
 * @author ellen.li
 * 
 */
public class MenuAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 6315207339655573356L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(MenuAction.class);
	@Autowired
	private DomainService<Menu> menuService;
	private SimplePager pager;
	private List<Menu> menus;
	private String menuKeyParam;
	private Map<String, Option> options = new HashMap<String, Option>();
	private Menu menu;
	private List<Option> optionCodes;

	/* 頁面參數 */
	private String menuKey;
	private String menuDesc;
	private String menuType;
	private String menuMemo;
	private String saveOrdUpdate;
	private String[] optionId;
	private String[] optionCode;
	private String[] optionMemoOne;
	private String[] optionMemoTwo;
	private String[] optionName;
	private String[] optionSortOrder;
	private String[] optionDelete;
	private String editType;
	private CommonCriteria crit;

	public String add() throws CoreException {
		menu = new Menu();
		menuKey = "";
		menuDesc = "";
		menuType = "";
		menuMemo = "";
		options = null;
		optionCodes = null;
		return "edit";
	}

	/* 編輯 */
	public String edit() throws Exception {
		options = new HashMap<String, Option>();
		if (StringUtils.isNotBlank(menuKey)) {
			menu = null;
			menu = menuService.get(menuKey);
			options = menu.getOptions();
			optionCodes = new ArrayList();
			for (Option arrayTemplate : options.values()) {
				optionCodes.add(arrayTemplate);
			}
		}
		return "edit";
	}

	public String editChannel() throws Exception {
		options = new HashMap<String, Option>();
		menu = null;
		menu = menuService.get("Channel.Name");
		options = menu.getOptions();
		optionCodes = new ArrayList<Option>();
		for (Option arrayTemplate : options.values()) {
			optionCodes.add(arrayTemplate);
		}
		return "editChannel";
	}

	public String getEditType() {
		return editType;
	}

	public Menu getMenu() {
		return menu;
	}

	public String getMenuDesc() {
		return menuDesc;
	}

	public String getMenuKey() {
		return menuKey;
	}

	public String getMenuKeyParam() {
		return menuKeyParam;
	}

	public String getMenuMemo() {
		return menuMemo;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public String getMenuType() {
		return menuType;
	}

	public String[] getOptionCode() {
		return optionCode;
	}

	public List<Option> getOptionCodes() {
		return optionCodes;
	}

	public String[] getOptionDelete() {
		return optionDelete;
	}

	public String[] getOptionId() {
		return optionId;
	}

	public String[] getOptionMemoOne() {
		return optionMemoOne;
	}

	public String[] getOptionMemoTwo() {
		return optionMemoTwo;
	}

	public String[] getOptionName() {
		return optionName;
	}

	public Map<String, Option> getOptions() {
		return options;
	}

	public String[] getOptionSortOrder() {
		return optionSortOrder;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getSaveOrdUpdate() {
		return saveOrdUpdate;
	}

	public String list() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(menuKeyParam)) {
			crit.addRlike("key", menuKeyParam.trim());
		}
		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = menuService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);
		menus = menuService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit, null);
		// logger.info("pager:{},menus:{}", pager, menus);
		return "list";
	}

	public String listYekmobile() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isBlank(menuKeyParam)) {
			menuKeyParam = "mobile" ;
		}
		crit.addRlike("key", menuKeyParam.trim());
		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = menuService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);
		menus = menuService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), crit, null);
		// logger.info("pager:{},menus:{}", pager, menus);
		return "listYekmobile";
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}

	/* 新增,删除,修改 */
	public String save() throws Exception {
		if (null != optionDelete) { // delete
			Menu tmpMmenu = new Menu();
			tmpMmenu = menuService.get(menuKey);
			options = tmpMmenu.getOptions();
			for (int i = 0; i < optionDelete.length; i++) {
				tmpMmenu.getOptions().remove(optionDelete[i]);
				menuService.save(tmpMmenu);
			}
			optionDelete = null;
		} else {
			Menu tmpMmenu = new Menu();
			Map<String, Option> tmpOptions = new HashMap<String, Option>();
			if (StringUtils.isNotBlank(menuKey) && saveOrdUpdate.equals("update")) {
				tmpMmenu = menuService.get(menuKey);
				tmpMmenu.setKey(menuKey);
				tmpMmenu.setDescription(menuDesc);
				tmpMmenu.setType(menuType);
				tmpMmenu.setMemo(menuMemo);
				tmpOptions = tmpMmenu.getOptions();
			} else {
				tmpMmenu = new Menu();
			}
			for (int i = 0; i < optionCode.length; i++) {
				if (!optionId[i].equals("")) {
					for (Option option : tmpOptions.values()) {
						if (!optionId[i].equals("")) {
							if (option.getOid().equals(Long.valueOf(optionId[i]))) {
								option.setCode(optionCode[i]);
								option.setMemo1(optionMemoOne[i]);
								option.setMemo2(optionMemoTwo[i]);
								option.setName(optionName[i]);
								option.setSortOrder(Integer.parseInt(optionSortOrder[i]));
								tmpOptions.put(optionCode[i], option);
							}
						}
					}
					tmpMmenu.setOptions(tmpOptions);
					tmpMmenu = menuService.save(tmpMmenu);
				} else {// save
					if (StringUtils.isNotBlank(optionCode[i])) {
						if (StringUtils.isNotBlank(menuKey) && saveOrdUpdate.equals("update")) {
							if (StringUtils.isNotBlank(optionSortOrder[i])) {
								tmpMmenu.addOption(optionCode[i], optionName[i], Integer.parseInt(optionSortOrder[i]),
										optionMemoOne[i], optionMemoTwo[i]);
							} else {
								tmpMmenu.addOption(optionCode[i], optionName[i], optionMemoOne[i], optionMemoTwo[i]);
							}
							menuService.save(tmpMmenu);
						} else {// for first insert
							tmpMmenu = new Menu();
							tmpMmenu.setKey(menuKey);
							tmpMmenu.setDescription(menuDesc);
							tmpMmenu.setType(menuType);
							tmpMmenu.setMemo(menuMemo);
							if (StringUtils.isNotBlank(optionSortOrder[i])) {
								tmpMmenu.addOption(optionCode[i], optionName[i], Integer.parseInt(optionSortOrder[i]),
										optionMemoOne[i], optionMemoTwo[i]);
							} else {
								tmpMmenu.addOption(optionCode[i], optionName[i], optionMemoOne[i], optionMemoTwo[i]);
							}
							menuService.save(tmpMmenu);
							setSaveOrdUpdate("update");
							setMenuKey(menuKey);
						}
					}
				}
			}
			options = null;
			menu = null;
		}
		return edit();
	}

	/* 新增,删除,修改 */
	public String saveChannel() throws Exception {
		menuKey = "Channel.Name";
		Menu tmpMmenu = new Menu();
		Map<String, Option> tmpOptions = new HashMap<String, Option>();
		tmpMmenu = menuService.get(menuKey);
		tmpOptions = tmpMmenu.getOptions();
		for (int i = 0; i < optionCode.length; i++) {
			if (StringUtils.isNotBlank(optionId[i])) {
				for (Option option : tmpOptions.values()) {
					if (StringUtils.isNotBlank(optionId[i])) {
						if (option.getOid().equals(Long.valueOf(optionId[i]))) {
							option.setCode(optionCode[i]);
							option.setName(optionName[i]);
							option.setMemo1(optionMemoOne[i]);
							tmpOptions.put(optionCode[i], option);
						}
					}
				}
				tmpMmenu.setOptions(tmpOptions);
				tmpMmenu = menuService.save(tmpMmenu);
			} else {// save
				if (StringUtils.isNotBlank(optionCode[i])) {
					if (StringUtils.isNotBlank(menuKey)) {
						tmpMmenu.addOption(optionCode[i], optionName[i], optionMemoOne[i], null);
						menuService.save(tmpMmenu);
					}
				}
			}
		}
		options = null;
		menu = null;
		return editChannel();
	}

	public void setEditType(String editType) {
		this.editType = editType;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public void setMenuDesc(String menuDesc) {
		this.menuDesc = menuDesc;
	}

	public void setMenuKey(String menuKey) {
		this.menuKey = menuKey;
	}

	public void setMenuKeyParam(String menuKeyParam) {
		this.menuKeyParam = menuKeyParam;
	}

	public void setMenuMemo(String menuMemo) {
		this.menuMemo = menuMemo;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	public void setOptionCode(String[] optionCode) {
		this.optionCode = optionCode;
	}

	public void setOptionCodes(List<Option> optionCodes) {
		this.optionCodes = optionCodes;
	}

	public void setOptionDelete(String[] optionDelete) {
		this.optionDelete = optionDelete;
	}

	public void setOptionId(String[] optionId) {
		this.optionId = optionId;
	}

	public void setOptionMemoOne(String[] optionMemoOne) {
		this.optionMemoOne = optionMemoOne;
	}

	public void setOptionMemoTwo(String[] optionMemoTwo) {
		this.optionMemoTwo = optionMemoTwo;
	}

	public void setOptionName(String[] optionName) {
		this.optionName = optionName;
	}

	public void setOptions(Map<String, Option> options) {
		this.options = options;
	}

	public void setOptionSortOrder(String[] optionSortOrder) {
		this.optionSortOrder = optionSortOrder;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setSaveOrdUpdate(String saveOrdUpdate) {
		this.saveOrdUpdate = saveOrdUpdate;
	}
}
